package com.hellobj.demo.packagehelper.controller;

import com.alibaba.fastjson.JSON;
import com.hellobj.demo.packagehelper.model.ExpressType;
import com.hellobj.demo.packagehelper.model.LogisticsDetails;
import com.hellobj.demo.packagehelper.model.Traces;
import com.hellobj.demo.packagehelper.service.KdApiOrderDistinguish;
import com.hellobj.demo.packagehelper.service.KdniaoTrackQueryAPI;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.logging.Logger;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;

/**
 * @author hello
 * @date 2018/6/19
 */
@RestController
public class Res {
    @PostMapping("/getPackDetails")
    public Object getPackDetails(@Param("number") String number){
        LogisticsDetails logisticsDetails=null;
        KdniaoTrackQueryAPI api = new KdniaoTrackQueryAPI();
        try {
            KdApiOrderDistinguish kdapi=new KdApiOrderDistinguish();
            String resultCode = kdapi.getOrderTracesByJson(number);
            ExpressType expressType =JSON.parseObject(resultCode,ExpressType.class);
            String result = api.getOrderTracesByJson(expressType.getShippers().get(0).getShipperCode(), number);

            logisticsDetails=JSON.parseObject(result,LogisticsDetails.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Logger logger=Logger.getLogger(Res.class.getName());
        logger.info(logisticsDetails.toString());
        return JSON.toJSON(logisticsDetails.getTraces());
    }

    @PostMapping("/test")
    public String test(@Param("number") String number){
        return number;
    }
}
